package controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import multiformat.BinaryBase;
import multiformat.Calculator;
import multiformat.DecimalBase;
import multiformat.HexBase;
import multiformat.OctBase;

public class BaseController extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
			
	private JComboBox<String> cmbBases;
	private JLabel lblTitle;
	private Calculator model;
	
	public BaseController(Calculator model) {
		this.model = model;
		
		setLayout(new BorderLayout());
		
		String[] bases = {"Decimal","Binary","Hex","Oct"};
		
		lblTitle = new JLabel("<html><u>Base:</u></html>");
		lblTitle.setForeground(Color.red);
		
		cmbBases = new JComboBox<String>(bases);
		cmbBases.setSelectedIndex(0);
		
		add(lblTitle,BorderLayout.NORTH);
		add(cmbBases,BorderLayout.CENTER);
		
		cmbBases.addActionListener(this);
		
		validate();
		setVisible(true);
	}


	@SuppressWarnings("unchecked")
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		JComboBox<String> selected = null;
		
		if(source instanceof JComboBox<?>){
			selected = (JComboBox<String>) source;
			
			int index = selected.getSelectedIndex();
	      		      
			if(index == 0) {
				//Decimal selected
				model.setBase(new DecimalBase());
				
			}else if(index == 1){
				//Binary selected
				model.setBase(new BinaryBase());			
			}else if(index == 2){
				//Hex selected
				model.setBase(new HexBase());
			}else if(index == 3) {
				//Oct selected
				model.setBase(new OctBase());	
			}
		}else{
			// Do nothing.
		}
		
	}

}
